package com.zzty.txj.util;



import com.zzty.txj.entity.*;
import com.zzty.txj.service.ClazzService;
import com.zzty.txj.service.PostService;
import com.zzty.txj.service.RecordService;
import com.zzty.txj.service.SearchService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class ScheduledUtil {
    private Logger logger=LoggerFactory.getLogger(ScheduledUtil.class);
    @Autowired
    ClazzService clazzServiceImpl;
@Autowired
    RecordService recordServiceImpl;
    @Autowired
    PostService postService;
    @Autowired
    SearchService searchService;

    @Scheduled(cron="0 0 23 ? * SUN")
    //@Scheduled(cron="0 0 10 ? 1/1 ? ")
    //@Scheduled(cron="*/5 * * * * ?" )
    public void getDoEventByDate() throws Exception {
        List<Clazz> clazzes=clazzServiceImpl.getClazz();
        System.out.println("======"+clazzes);
        logger.info("===initialDelay: clazzes is " + clazzes);
         for (Clazz clazz : clazzes) {
            List<List<WeekRecord>> clazzList=null;
            List<User> users=clazzServiceImpl.getUsers(clazz);
            logger.info("===initialDelay: users is " + users);
            for (User user : users) {
                if(clazzList==null){
                    clazzList=new ArrayList<List<WeekRecord>>();
                }
                List<WeekRecord> weekRecords=null;
                List<DoDaily> doDailies=recordServiceImpl.getWeek(user);
                logger.info("===initialDelay: doDailies is " + doDailies);
                for (DoDaily d1 : doDailies) {
                    if (weekRecords == null) {
                        weekRecords=new ArrayList<WeekRecord>();
                    }
                   // SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
                   // String date=sdf.format(d1.getDate());
                    WeekRecord weekRecord=new WeekRecord();
                   // weekRecord.setDate(date);
                    weekRecord.setDate(d1.getDate());
                    weekRecord.setDailyName(recordServiceImpl.getDailyName(d1.getDailyId()));
                    weekRecord.setUserName(user.getName());
                    if (1==d1.getResult()) {
                        weekRecord.setResult("已打卡");
                    } else {
                        if (0==d1.getResult()) {
                            weekRecord.setResult("请假");
                        }
                        weekRecord.setResult("未打卡");
                    }
                    if (1==d1.getFinalResult()) {
                        weekRecord.setFinalResult("打卡成功");
                    } else {
                        if (0==d1.getFinalResult()) {
                            weekRecord.setFinalResult("取消勤学金资格");
                        } else {
                            weekRecord.setFinalResult("打卡失败");
                        }
                    }
                    logger.info("===initialDelay: weekRecord is " + weekRecord);
                    weekRecords.add(weekRecord);
                    clazzList.add(weekRecords);
                }
                logger.info("===initialDelay: weekRecords is " + weekRecords);
                logger.info("===initialDelay: clazzList is " + clazzList);


                if ("teacher".equals(user.getType())) {
                    continue;
                }
                String str=listToTable(weekRecords);
                logger.info("===initialDelay:str is " + str);
                EmailUtil emailUtil=new EmailUtil();
                emailUtil.sendEmail(user.getEmail(), str, user.getName());
            }
        }
    }
    private String listToTable(List<WeekRecord> weekRecords){
        if(weekRecords==null){
            weekRecords=new ArrayList<WeekRecord>();
        }
        StringBuilder str=new StringBuilder();
        for(WeekRecord weekRecord:weekRecords){
            str.append("<tr><td align='center'>"+weekRecord.getUserName()+"</td><td align='center'>"+weekRecord.getDate() +"</td><td align='center'>"
                    +weekRecord.getDailyName() +"</td><td align='center'>"+weekRecord.getResult()+"</td><td align='center'>"+weekRecord.getFinalResult()+"</td></tr>");
        }
        return String.valueOf(str);
    }

    //@Scheduled(cron="*/15 * * * * ?" )
  //@Scheduled(cron="0 27-28 09 * * ?" )//发布每天定时
    @Scheduled(cron = "0 0 2 ? * MON,WED,FRI,SAT,SUN")
    public void dailies(){
        List<UserDaily>userDailies=postService.getDailyName1();
        String dailyId;
        String name;
        Integer length=null;
        Date date=new Date();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String date1=String.valueOf(df1.format(date));
        for(int i=0;i<userDailies.size();i++){
            dailyId=userDailies.get(i).getDailyId().replaceAll(",","");
            Integer userId=userDailies.get(i).getUserId();
            for(int j=0;j<dailyId.length();j++){
                char a[]=dailyId.toCharArray();
                String b=String.valueOf(a[j]);
                int c=Integer.valueOf(b);
                DoDaily doDaily=new DoDaily();
                doDaily.setDate(date1);
                doDaily.setUserId(userId);
                doDaily.setDailyId(c);
                doDaily.setResult(-1);
                Integer count=postService.count(doDaily);
                System.out.println(count);
                name=postService.getDailyName(c);
                System.out.println("name"+name);

            }
        }
    }
    //@Scheduled(cron="0 12-21 10 * * ?" )
//@Scheduled(cron="*/20 * * * * ?" )
    @Scheduled(cron = "0 0 22 ? * MON,WED,FRI,SAT,SUN")
    public void getDoDaily(){
        Date day=new Date();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String day1=String.valueOf(df.format(day));
        List<DoDaily>doDailies=searchService.getDoDaily(day1);

        for(int i=0;i<doDailies.size();i++){
            DoDaily doDaily=new DoDaily();
            Integer userId=doDailies.get(i).getUserId();
            doDaily.setUserId(userId);
            doDaily.setDate(day1);
            List<DoDaily>counts=searchService.counts(doDaily);
            for(int j=0;j<counts.size();j++) {
                doDaily.setUserId(userId);
                doDaily.setDailyId(counts.get(j).getDailyId());
                doDaily.setDate(day1);
                doDaily.setFinalResult(counts.get(j).getResult());
                searchService.count(doDaily);
            }
            for(int j=0;j<counts.size();j++){
                    if(counts.get(j).getDailyId()==4 &&counts.get(j).getResult()==-1){
                        doDaily.setUserId(userId);
                        doDaily.setDate(day1);
                        doDaily.setFinalResult(0);
                        searchService.count1(doDaily);
                        break;
                    }
                if(counts.get(j).getDailyId()==7 &&counts.get(j).getResult()==-1){
                    doDaily.setUserId(userId);
                    doDaily.setDate(day1);
                    doDaily.setFinalResult(0);
                    searchService.count1(doDaily);
                    break;
                }
                if(counts.get(j).getDailyId()==8 &&counts.get(j).getResult()==-1){
                    doDaily.setUserId(userId);
                    doDaily.setDate(day1);
                    doDaily.setFinalResult(0);
                    searchService.count1(doDaily);
                    break;
                }
                if(counts.get(j).getDailyId()==9 &&counts.get(j).getResult()==-1){
                    doDaily.setUserId(userId);
                    doDaily.setDate(day1);
                    doDaily.setFinalResult(0);
                    searchService.count1(doDaily);
                    break;
                }
            }
        }
    }
}
